﻿using CommunityToolkit.Mvvm.ComponentModel;
using MetaView.Main.Models;
using System;
using System.Collections.Generic;
using System.Windows.Media;

namespace MetaView.Main.ViewModels
{
    public partial class DataViewModel : ViewModel
    {
        private bool _isInitialized = false;

        [ObservableProperty]
        private List<DataColor> _colorItems = new List<DataColor>();

        public override void OnNavigatedTo()
        {
            if (!_isInitialized)
            {
                InitializeViewModel();
            }
        }

        private void InitializeViewModel()
        {
            var random = new Random();
            _colorItems.Clear();

            // 预分配列表容量减少内存重分配
            if (_colorItems.Capacity < 8192)
                _colorItems.Capacity = 8192;

            for (int i = 0; i < 8192; i++)
            {
                _colorItems.Add(
                    new DataColor
                    {
                        Color = new SolidColorBrush(
                            Color.FromArgb(
                                (byte)200,
                                (byte)random.Next(0, 250),
                                (byte)random.Next(0, 250),
                                (byte)random.Next(0, 250)
                            )
                        ),
                    }
                );
            }

            _isInitialized = true;
        }
    }
}
